//
//  ActionsController.m
//  YSSmartHome
//
//  Created by yanghao on 16/9/13.
//  Copyright © 2016年 yahaong. All rights reserved.
//

#import "ActionsController.h"
#import "MGSwipeTableCell.h"
#import "JXMovableCellTableView.h"
#import "YHMarcros.h"

@interface ActionsController ()
<
JXMovableCellTableViewDataSource,
JXMovableCellTableViewDelegate,
MGSwipeTableCellDelegate
>

@property(nonatomic, strong)JXMovableCellTableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataSource;
@end

@implementation ActionsController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"启动动作";
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * reuseIdentifier = @"programmaticCell";
    MGSwipeTableCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[MGSwipeTableCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifier];
    }
    cell.textLabel.text = self.dataSource[indexPath.row];
//    cell.leftSwipeSettings.transition = data.transition;
//    cell.rightSwipeSettings.transition = data.transition;
//    cell.leftExpansion.buttonIndex = data.leftExpandableIndex;
//    cell.leftExpansion.fillOnTrigger = NO;
//    cell.rightExpansion.buttonIndex = data.rightExpandableIndex;
//    cell.rightExpansion.fillOnTrigger = YES;
    cell.rightButtons = [self createRightButtons:2];
    cell.delegate = self;
    return cell;
}

-(NSArray *) createRightButtons: (int) number
{
    NSMutableArray * result = [NSMutableArray array];
    UIColor * colors[2] = {RGBColor(239, 75, 58),
        RGBColor(204, 204, 204)};
    UIImage * icons[2] = {[UIImage imageNamed:@"scene_bigdel"], [UIImage imageNamed:@"scene_bigedit"]};
    for (int i = 0; i < number; ++i)
    {
        MGSwipeButton * button = [MGSwipeButton buttonWithTitle:@"" icon:icons[i] backgroundColor:colors[i] padding:15 callback:^BOOL(MGSwipeTableCell * sender){
            debugLogInfo(@"Convenience callback received (left).");
            return YES;
        }];
        [result addObject:button];
    }
    return result;
}

-(BOOL) swipeTableCell:(MGSwipeTableCell*) cell tappedButtonAtIndex:(NSInteger) index direction:(MGSwipeDirection)direction fromExpansion:(BOOL) fromExpansion
{
    debugLogInfo(@"Delegate: button tapped, %@ position, index %d, from Expansion: %@",
          direction == MGSwipeDirectionLeftToRight ? @"left" : @"right", (int)index, fromExpansion ? @"YES" : @"NO");
    
    if (direction == MGSwipeDirectionRightToLeft && index == 0) {
        //delete button
        NSIndexPath * path = [_tableView indexPathForCell:cell];
        [_dataSource removeObjectAtIndex:path.row];
        [_tableView deleteRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationLeft];
        return NO; //Don't autohide to improve delete expansion animation
    }
    
    return YES;
}
- (NSArray *)dataSourceArrayInTableView:(JXMovableCellTableView *)tableView
{
    return _dataSource.copy;
}

- (void)tableView:(JXMovableCellTableView *)tableView newDataSourceArrayAfterMove:(NSArray *)newDataSourceArray
{
    _dataSource = newDataSourceArray.mutableCopy;
}

- (NSMutableArray *)dataSource
{
    if (!_dataSource) {
        _dataSource = [NSMutableArray new];
        for (NSInteger section = 0; section < 16; section ++) {
                [_dataSource addObject:[NSString stringWithFormat:@"section -- %ld row -- %ld", section, section]];
        }
    }
    return _dataSource;
}
- (JXMovableCellTableView *)tableView
{
    if (!_tableView) {
        _tableView = [[JXMovableCellTableView alloc]init];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.gestureMinimumPressDuration = 1.0;
        _tableView.drawMovalbeCellBlock = ^(UIView *movableCell){
            movableCell.layer.shadowColor = [UIColor grayColor].CGColor;
            movableCell.layer.masksToBounds = NO;
            movableCell.layer.cornerRadius = 0;
            movableCell.layer.shadowOffset = CGSizeMake(-5, 0);
            movableCell.layer.shadowOpacity = 0.4;
            movableCell.layer.shadowRadius = 5;
        };
    }
    return _tableView;
}
@end
